package loengud.w11.simpsons;

public class CookieJar {

	private int contents;
	private boolean available = false;

	public synchronized void getCookie(String who) {
		System.out.println("getCookie");
		while (!available) {
			try {
				System.out.println("getCookie wait");
				wait();
			} catch (InterruptedException e) {
			}
		}

		available = false;
		notifyAll();
		System.out.println(who + " ate cookie " + contents);
	}

	public synchronized void putCookie(String who, int value) {
		System.out.println("putCookie");

		while (available) {
			try {
				System.out.println("putCookie wait");
				wait();
			} catch (InterruptedException e) {
			}
		}

		contents = value;
		available = true;
		System.out.println(who + " put cookie " + contents + " in the jar");
		notifyAll();
	}

	void arrayShift(byte[] array, int count) {
		synchronized (array) {
			System.arraycopy(array, count, 
					array, 0, array.length - count);
		}
	}

}
